package com.pojo.item.controller;


import com.pojo.item.config.JdbcConfigBean;
import com.pojo.item.entity.Reservation;
import com.pojo.item.entity.Ticket;
import com.pojo.item.service.ReservationService;
import com.pojo.item.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class ReservationController {
    @Autowired
    private ReservationService reservationService;

    @GetMapping(value = "item/queryReservationByUserId/{id}")
    public Map<Long, Reservation> queryReservationByUserId(@PathVariable("id") Long id) {
        return this.reservationService.queryReservationByUserId(id);
    }


    @GetMapping(value = "/item/reserveTicket/{userId}/{t_id}")
    public String reserveTicket(@PathVariable("userId") Long userId, @PathVariable("t_id") Long t_id) {
        if (new TicketService().queryTicketById(t_id) != null && new TicketService().queryTicketById(t_id).getTicket_state() == 0) {
            this.reservationService.reserveTicket(userId, t_id);
            return "reserve successfully!";
        }
        return "reserve failed!";
    }

    @GetMapping(value = "/item/cancelReservation/{userId}/{t_id}")
    public String cancelReservation(@PathVariable("userId") Long userId, @PathVariable("t_id") Long t_id) {
        if (new TicketService().queryTicketById(t_id).getTicket_state() == 1 && reservationService.queryReservationByUserId(userId).get(t_id) != null) {
            this.reservationService.cancelReservation(userId, t_id);
            return "cancel successfully!";
        }
        return "failed!";

    }
}

